package com.lei.demo.controller;

import com.lei.demo.entity.Budget;
import com.lei.demo.entity.TotalForm;
import com.lei.demo.entity.TripForm;
import com.lei.demo.entity.User;
import com.lei.demo.service.BudgetService;
import com.lei.demo.service.TotalFormService;
import com.lei.demo.service.TripFormService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Shinelon on 2017/6/16.
 */
@Controller
@RequestMapping("/budget")
public class BudgetController {

    @Autowired
    BudgetService budgetService;
    TotalFormService totalFormService;
    TripFormService tripFormService;

    @RequestMapping(value = "/handup",method = {RequestMethod.GET,RequestMethod.POST})
    public  String handUpBudget(Model model, Budget budget, HttpServletRequest request) {
        model.addAttribute("status", "exist");
        User user = (User)request.getSession().getAttribute("user");
        budget.setUsername(user.getName());
        budgetService.save(budget);
        model.addAttribute("saved", "success");
        return "budget_new";
    }

    @RequestMapping(value = "/increase",method = {RequestMethod.GET,RequestMethod.POST})
    public String increaseBudget(Model model,Budget budget){
        model.addAttribute("status", "exist");
        return "budget_new";
    }

    @RequestMapping(value = "/budget",method = {RequestMethod.GET,RequestMethod.POST})
    private String budget(Model model,Budget budget) {
        model.addAttribute("status", "exist");
        return "budget_new";
    }

    @RequestMapping(value = "/update",method = {RequestMethod.GET,RequestMethod.POST})
    public String update(Model model, Budget budget) {
        budgetService.save(budget);
        model.addAttribute("saved", "success");
        return "budget_edit";
    }

    @RequestMapping(value = "/trip",method = {RequestMethod.GET,RequestMethod.POST})
    public String tripForm(Model model, TripForm tripForm) {
        return "tripForm_balance_check";
    }

    @RequestMapping(value = "/total",method = {RequestMethod.GET,RequestMethod.POST})
    public String totalForm(Model model, TotalForm totalForm) {
        return "totalForm_balance_check";
    }

    @RequestMapping(value = "/check",method = {RequestMethod.GET,RequestMethod.POST})
    public String checkBudget(Model model, HttpServletRequest request) {
        User user = (User)request.getSession().getAttribute("user");
        List<Budget> budget = budgetService.findBudgets(user.getName());
        model.addAttribute("bud", budget);
        return "budget_check";
    }

    @RequestMapping(value = "/delete/{buid}",method = {RequestMethod.GET,RequestMethod.POST})
    public String deleteBudget(Model model,@PathVariable int buid){
        List<Budget> budget=budgetService.findBudgets(buid);
        model.addAttribute("budget",budget);
        budgetService.delete(buid);
        model.addAttribute("deleted", "success");
        model.addAttribute("budget",new Budget());
        return "budget_check";
    }

    @RequestMapping(value = "/edit/{buid}",method = {RequestMethod.GET,RequestMethod.POST})
    public String updatebudget(Model model,@PathVariable int buid){
        Budget budget=budgetService.findById(buid);
        model.addAttribute("budget",budget);
        return "budget_edit";
    }

    @RequestMapping(value = "/checktripform",method = {RequestMethod.GET,RequestMethod.POST})
    public String checkTripForm(Model model) {
        return null;
    }

    @RequestMapping(value = "/checktotalform",method = {RequestMethod.GET,RequestMethod.POST})
    public String checkTotalForm(Model model) {
        return null;
    }

    @RequestMapping(value = "/print",method = {RequestMethod.GET,RequestMethod.POST})
    public String print(Model model) {
        return null;
    }

}
